<?php
/***************************************************************
* Subs-XML.php                                                 *
*--------------------------------------------------------------*
* SMF Mod Creator                                              *
* Version 0.1 Alpha                                            *
* Written By: Labradoodle-360                                  *
*--------------------------------------------------------------*
* Copyright 2011 Matthew Kerle - All Rights Reserved           *
*--------------------------------------------------------------*
* Redistribution and use in source and binary forms, with or   *
* without modification, are permitted provided that the        *
* conditions specified in the attached                         *
* smfmodcreator_license.txt file are met.                      *
***************************************************************/
if (!defined('SMF'))
	die('Hacking Attempt...');


function xmlgenerate_package_info($modID = '', $fields = array())
{

	// Globalize everything we'll need here...
	global $boardurl, $boarddir;

	// We shouldn't HAVE empty fields all the way here, oh well.
	if (empty($fields) || empty($modID))
		return;

	// Actually create the new directory!
	mkdir($boarddir . '/smfmodcreator/modifications/' . $fields['identifier'], 0755);

	// These couple lines copy an index.php template, and add it to the directory. = No directory snooping
	$index_file_contents = file_get_contents($boardurl . '/smfmodcreator/resources/index.template.txt');
	file_put_contents($boarddir . '/smfmodcreator/modifications/' . $fields['identifier'] . '/index.php', $index_file_contents);

	// Create & Populate package-info.xml
	$illm = new DOMDocument('1.0');
	$illm->formatOutput = true;
	$head_text = $illm->createComment(' This package was generated by the SMF Mod Creator, <http://code.google.com/p/smf-mod-creator>, which is developed and supported by: <http://code.google.com/p/smf-mod-creator/people/list>. ');
	$package_info = $illm->appendChild($head_text);
	$package_info = $illm->createElement('package-info');
	$xmlns = $illm->appendChild($package_info);
	$xmlns->setAttribute('xmlns', 'http://www.simplemachines.org/xml/package-info');
	$xmlns->setAttribute('xmlns:smf', 'http://www.simplemachines.org/');
	/* XML Name Comment */
	$name_comment = $illm->createComment(' Package Display Name ');
	$name_comment_append = $package_info->appendChild($name_comment);
	/* DOM: Package Name */
	$name = $illm->createElement('name', $fields['mod_name']);
	$name = $package_info->appendChild($name);
	/* XML ID Comment */
	$id_comment = $illm->createComment(' Package ID - AuthorName:PackageName ');
	$id_comment_append = $package_info->appendChild($id_comment);
	/* DOM: Package ID */
	$id = $illm->createElement('id', $fields['author_name'] . ':' . $fields['identifier']);
	$id = $package_info->appendChild($id);
	/* XML Version Comment */
	$version_comment = $illm->createComment(' Package Version ');
	$version_comment_append = $package_info->appendChild($version_comment);
	/* DOM: Version !! For now it's static !! */
	$version = $illm->createElement('version', '1.0');
	$version = $package_info->appendChild($version);
	/* XML Type Comment */
	$type_comment = $illm->createComment(' Package Type ');
	$type_comment_append = $package_info->appendChild($type_comment);
	/* DOM: Type !! This will changes when themes come into play */
	$type = $illm->createElement('type', 'modification');
	$type = $package_info->appendChild($type);
	/* Proceed with Formatting */
	$formattedXML = $illm->saveXML();
	/* A line break was being added to the end of each file... */
	$formattedXML = rtrim($formattedXML, "\n");
	file_put_contents($boarddir . '/smfmodcreator/modifications/' . $fields['identifier'] . '/package-info.xml', $formattedXML);
	log_file_creation($modID, 'package-info', 'xml', '0', $fields['author_id'], $fields['submitted']);

}

/* This function is for returning SimpleXML - From: http://changelog.ca/log/2005/06/14/php-simplexml-cdata-problem--and-my-solution */
function uncdata($xml = '')
{

	// Come on...
	if (empty($xml))
		return;

		// States:
		//
		//     'out'
		//     '<'
		//     '<!'
		//     '<!['
		//     '<![C'
		//     '<![CD'
		//     '<![CDAT'
		//     '<![CDATA'
		//     'in'
		//     ']'
		//     ']]'
		//
		// (Yes, the states a represented by strings.) 
		//

	// Three quick variables.
	$state = 'out';
	$a = str_split($xml);
	$new_xml = '';

	// Our foreach statement...
	foreach ($a as $k => $v)
	{

		// Deal with "state".
		switch ($state)
		{
			case 'out':
			if ($v == '<')
			{
				$state = $v;
			}
			else
			{
				$new_xml .= $v;
			}
			break;

			case '<':
			if ($v == '!')
			{
				$state = $state . $v;
			}
			else
			{
				$new_xml .= $state . $v;
				$state = 'out';
			}
			break;

			case '<!':
			if ($v == '[')
			{
				$state = $state . $v;
			}
			else
			{
				$new_xml .= $state . $v;
				$state = 'out';
			}
			break;

			case '<![':
			if ($v == 'C')
			{
				$state = $state . $v;
			}
			else
			{
				$new_xml .= $state . $v;
				$state = 'out';
			}
			break;

			case '<![C':
			if ($v == 'D')
			{
				$state = $state . $v;
			}
			else
			{
				$new_xml .= $state . $v;
				$state = 'out';
			}
			break;

			case '<![CD':
			if ($v == 'A')
			{
				$state = $state . $v;
			}
			else
			{
				$new_xml .= $state . $v;
				$state = 'out';
			}
			break;

			case '<![CDA':
			if ($v == 'T')
			{
				$state = $state . $v;
			}
			else
			{
				$new_xml .= $state . $v;
				$state = 'out';
			}
			break;

			case '<![CDAT':
			if ($v == 'A')
			{
				$state = $state . $v;
			}
			else
			{
				$new_xml .= $state . $v;
				$state = 'out';
			}
			break;

			case '<![CDATA':
			if ($v == '[')
			{
				$cdata = '';
				$state = 'in';
			}
			else
			{
				$new_xml .= $state . $v;
				$state = 'out';
			}
			break;

			case 'in':
			if ($v == ']')
			{
				$state = $v;
			}
			else
			{
				$cdata .= $v;
			}
			break;

			case ']':
			if ($v == ']')
			{
				$state = $state . $v;
			}
			else
			{
				$cdata .= $state . $v;
				$state = 'in';
			}
			break;

			case ']]':
			if ($v == '>')
			{
				$new_xml .= str_replace('>','&gt;', str_replace('>','&lt;', str_replace('"','&quot;', str_replace('&','&amp;', $cdata))));
				$state = 'out';
			}
			else
			{
				$cdata .= $state . $v;
				$state = 'in';
			}
			break;
		}
	}

	// Return $new_xml!
	return $new_xml;

}

?>